package com.base.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.sqlparam.OperationlogsExamplePage;
import com.base.po.voip.Operationlogs;
import com.base.po.voip.Users;
import com.base.po.voip.OperationlogsExample.Criteria;
import com.base.service.LogsService;
import com.base.service.UsersService;
import com.base.utils.IP;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_huishoubaobiao {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private UsersService usersService;

	// 回收报表
	@RequestMapping("/content_huishoubaobiao")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		OperationlogsExamplePage examplePage = new OperationlogsExamplePage();
		// UsersExample userExample = new UsersExample();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		examplePage.setOrderByClause("CreateTime");
		List<Operationlogs> operationlogs = null;
		Users user = null;
		Criteria criteria = examplePage.createCriteria();
		// admin 显示所有
		if (adminUsergrade == 0) {
			examplePage.getOredCriteria().add(criteria.andTypeEqualTo(6));
			operationlogs = logsService.selectByExamplePage(examplePage);
		}
		// OEM 显示其回日志
		if (adminUsergrade == 1) {
			user = usersService.selectByPrimaryKey(adminUserId);
			examplePage.getOredCriteria().add(criteria.andTypeEqualTo(6).andNameEqualTo(user.getUsername()));
			operationlogs = logsService.selectByExamplePage(examplePage);
		}
		/*
		 * 设置分页
		 */
		int totlenum = logsService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", 1);// 当前页面传入session中

		if (operationlogs != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huishoubaobiao");
			request.setAttribute("list", operationlogs);
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "回收报表信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	@RequestMapping("/selectHuiShouOperationlogs")
	public ModelAndView selectHuiShouOperationlogs(HttpServletRequest request, String name, Integer curPage, String timeFrom, String timeTo)
			throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (name == null) {
			if (request.getSession().getAttribute("name") != null) {
				name = (String) request.getSession().getAttribute("name");
			}
		}
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		OperationlogsExamplePage examplePage = new OperationlogsExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		examplePage.setOrderByClause("CreateTime");
		List<Operationlogs> operationlogs = null;
		Users user = null;
		Criteria criteria = examplePage.createCriteria();
		// 条件筛选
		if (name != null && !name.equals("")) {
			request.setAttribute("name", name);
			criteria = criteria.andNameLike("%" + name + "%").andTypeEqualTo(6);
			examplePage.getOredCriteria().add(criteria);
		}
		if (timeFrom != null && !timeFrom.equals("") && timeTo != null && !timeTo.equals("")) {
			request.setAttribute("timeFrom", timeFrom);
			request.setAttribute("timeTo", timeTo);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			criteria = criteria.andCreatetimeBetween(sdf.parse(timeFrom), sdf.parse(timeTo));
			examplePage.getOredCriteria().add(criteria);
		}
		// admin 显示所有
		if (adminUsergrade == 0) {
			examplePage.getOredCriteria().add(criteria.andTypeEqualTo(6));
			operationlogs = logsService.selectByExamplePage(examplePage);
		}
		// OEM 显示其回日志
		if (adminUsergrade == 1) {
			user = usersService.selectByPrimaryKey(adminUserId);
			examplePage.getOredCriteria().add(criteria.andTypeEqualTo(6).andNameEqualTo(user.getUsername()));
			operationlogs = logsService.selectByExamplePage(examplePage);
		}
		/*
		 * 设置分页
		 */
		int totlenum = logsService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", curPage);// 当前页面传入session中
		operationlogs = logsService.selectByExamplePage(examplePage);
		if (operationlogs != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huishoubaobiao");
			request.setAttribute("list", operationlogs);
            request.setAttribute("timeFrom", timeFrom);
            request.setAttribute("timeTo", timeTo);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}
